package com.service.impl;

import com.entry.SanShi;
import com.mapper.SanShiMapper;
import com.service.SanShiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Service
public class SanShiServiceImpl implements SanShiService {
    @Autowired
    private SanShiMapper sanShiMapper;
    @Override
    public List<SanShi> getSanShi() {
        LocalDate today = LocalDate.now();
        // 创建一个Map来存储日期和新增条数
        // 遍历前30天的日期，并初始化新增条数为0
        List<SanShi> list=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            LocalDate date = today.minusDays(i);
            DateTimeFormatter formatter=DateTimeFormatter.ofPattern("MM/dd");
            SanShi sanShi=new SanShi();
            sanShi.setDate(date.format(formatter));
            sanShi.setDanSum(0);
            sanShi.setChuLi(0);
            list.add(sanShi);
        }
        List<SanShi> sanShiList=  sanShiMapper.getSanShi();
        for (SanShi sanShi : list) {
            for (SanShi shi : sanShiList) {
                if(sanShi.getDate().equals(shi.getDate())){
                    sanShi.setDanSum(shi.getDanSum());
                    sanShi.setChuLi(shi.getChuLi());
                }
            }
        }

        return list;
    }

}
